package app.server.project.main.service.db.entity.user;

import app.server.project.core.constants.Def;
import app.server.project.core.syndb.SynDBMgr;
import app.server.project.main.service.db.entity.cfg.RaceCfgEntity;
import app.server.project.main.service.service.race.DetailStatusType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "user_race_detail")
@TableName("user_race_detail")
@Comment(value = "用户赛道明细")
public class UserRaceDetailEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    @JsonIgnore
    private long id;

    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;


    @TableField("rate")
    @Comment("比例")
    @Column(name = "rate")
    @JsonSerialize(using = ToStringSerializer.class)
    private long rate;


    @TableField("val")
    @Comment("数值")
    @Column(name = "val")
    @JsonSerialize(using = ToStringSerializer.class)
    private long val;

    @TableField("user_id")
    @Comment("用户id")
    @Column(name = "user_id")
    @JsonIgnore
    private long userId;

    @TableField("race_id")
    @Comment("赛道id")
    @Column(name = "race_id")
    private int raceId;

    @TableField("detail_type")
    @Comment("明细id")
    @Column(name = "detail_type")
    private int detailType;

    @TableField("add_type")
    @Comment("正数类型")
    @Column(name = "add_type")
    private int addType;

    @TableField("status")
    @Comment("审批状态")
    @Column(name = "status")
    private int status;

    @TableField("is_del")
    @Comment("删除类型")
    @Column(name = "is_del")
    private int isDel;

    @Transient
    @TableField(exist = false)
    private String detailTypeStr;

    @Transient
    @TableField(exist = false)
    private String detailStatusStr;


    @TableField("block_type")
    @Column(name = "block_type")
    @Comment("区块类型")
    private int blockType;

    @Transient
    @JsonIgnore
    @TableField(exist = false)
    private boolean synDb = false;

    public static UserRaceDetailEntity ofAddType(long id, long userId, int detailType, RaceCfgEntity raceCfgEntity, long val) {
        UserRaceDetailEntity userEnergyEntity = new UserRaceDetailEntity();
        userEnergyEntity.setId(id);
        userEnergyEntity.setSynDb(true);
        userEnergyEntity.setCreateTime(LocalDateTime.now());
        userEnergyEntity.setRate(raceCfgEntity.getRate());
        userEnergyEntity.setRaceId(raceCfgEntity.getId());
        userEnergyEntity.setDetailType(detailType);
        userEnergyEntity.setUserId(userId);
        userEnergyEntity.setVal(val);
        userEnergyEntity.setStatus(DetailStatusType.Credited.getType());
        userEnergyEntity.setAddType(Def.True);
        return userEnergyEntity;
    }

    public static UserRaceDetailEntity ofConsumeType(long id, long userId, int detailType, RaceCfgEntity raceCfgEntity, long val) {
        UserRaceDetailEntity userEnergyEntity = new UserRaceDetailEntity();
        userEnergyEntity.setId(id);
        userEnergyEntity.setSynDb(true);
        userEnergyEntity.setCreateTime(LocalDateTime.now());
        userEnergyEntity.setRate(raceCfgEntity.getRate());
        userEnergyEntity.setRaceId(raceCfgEntity.getId());
        userEnergyEntity.setDetailType(detailType);
        userEnergyEntity.setUserId(userId);
        userEnergyEntity.setVal(val);
        userEnergyEntity.setStatus(DetailStatusType.Pending.getType());
        userEnergyEntity.setAddType(Def.FALSE);
        return userEnergyEntity;
    }

    public void setStatus(int status) {
        this.status = status;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserRaceDetailEntity::getStatus, this);
    }

    public void setIsDel(int isDel) {
        this.isDel = isDel;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserRaceDetailEntity::getIsDel, this);
    }

    public void setRate(long rate) {
        this.rate = rate;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserRaceDetailEntity::getRate, this);
    }

    public void setVal(long val) {
        this.val = val;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserRaceDetailEntity::getVal, this);
    }

    public void setUserId(long userId) {
        this.userId = userId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserRaceDetailEntity::getUserId, this);
    }

    public void setRaceId(int raceId) {
        this.raceId = raceId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserRaceDetailEntity::getRaceId, this);
    }

    public void setDetailType(int detailType) {
        this.detailType = detailType;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserRaceDetailEntity::getDetailType, this);
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserRaceDetailEntity::getCreateTime, this);
    }

    public void setAddType(int addType) {
        this.addType = addType;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserRaceDetailEntity::getAddType, this);
    }

    public void setBlockType(int blockType) {
        this.blockType = blockType;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserRaceDetailEntity::getBlockType, this);
    }


}
